<template>
  <div class="select-none">
    <div class="tenant">
      <!--  租户选择 -->
      <!-- <tenant/> -->
    </div>
    <img :src="bg" class="wave"/>
    <div class="flex-c absolute right-5 top-3"></div>
    <div class="login-container01">
      <div class="login-box">
        <div class="login-form">
          <div class="logo-wrap">
            <img class="logo" src="/@/assets/login/logo05.png" alt="" srcset="">
          </div>
          <div class="text-6xl font-semibold title">IPRWORK 创新人才服务平台</div>
          <div class="flex justify-center self-center">
            <div class="bg-white mx-auto rounded-3xl w-96 ">
              <div class="space-y-0">
                <register v-if="loginType === LoginTypeEnum.REGISTER" @change="changeLoginType"/>
                <password v-if="loginType === LoginTypeEnum.PASSWORD" @signInSuccess="signInSuccess"
                          @change="changeLoginType"/>
                <mobile v-if="loginType === LoginTypeEnum.MOBILE" @signInSuccess="signInSuccess"
                        @change="changeLoginType"/>
                <expire v-if="loginType === LoginTypeEnum.EXPIRE" :username="username" @change="changeLoginType"/>
                <div class="flex items-center justify-center space-x-2 my-5">
                  <span class="h-px w-16 bg-gray-100"></span>
                  <span class="text-gray-300 font-normal">or</span>
                  <span class="h-px w-16 bg-gray-100"></span>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
      <!-- <div class="copyright">Copyright © 2024 IPRWORK 创新人才服务平台 All rights reserved. 技术支持：湖南微联信创</div> -->
    </div>
  </div>
</template>

<script setup lang="ts" name="loginIndex">
import {useThemeConfig} from '/@/stores/themeConfig';
import {NextLoading} from '/@/utils/loading';
import illustration from '/@/assets/login/login_bg.svg';
import bg from '/@/assets/login/bg03.png';
import miniQr from '/@/assets/login/mini_qr.png';
import {useI18n} from 'vue-i18n';
import {formatAxis} from '/@/utils/formatTime';
import {useMessage} from '/@/hooks/message';
import {Session} from '/@/utils/storage';
import {initBackEndControlRoutes} from '/@/router/backEnd';
import {LoginTypeEnum} from "/@/api/login";

// 引入组件
const Password = defineAsyncComponent(() => import('./component/password.vue'));
const Mobile = defineAsyncComponent(() => import('./component/mobile.vue'));
const Social = defineAsyncComponent(() => import('./component/social.vue'));
const Register = defineAsyncComponent(() => import('./component/register.vue'));
const Expire = defineAsyncComponent(() => import('./component/expire.vue'));
const Tenant = defineAsyncComponent(() => import('./component/tenant.vue'));

// 定义变量内容
const storesThemeConfig = useThemeConfig();
const {themeConfig} = storeToRefs(storesThemeConfig);
const {t} = useI18n();
const route = useRoute();
const router = useRouter();

// 登录方式
const loginType = ref(LoginTypeEnum.PASSWORD)
// 用户名
const username = ref('')

// 修改登录类型
const changeLoginType = (type: LoginTypeEnum,name?:string) => {
  loginType.value = type
  if(name){
    username.value = name
  }
}

// 获取布局配置信息
const getThemeConfig = computed(() => {
  return themeConfig.value;
});

// 登录成功后的跳转处理事件
const signInSuccess = async () => {
  const isNoPower = await initBackEndControlRoutes();
  if (isNoPower) {
    useMessage().warning('抱歉，您没有登录权限');
    Session.clear();
  } else {
    // 初始化登录成功时间问候语
    let currentTimeInfo = formatAxis(new Date());
    if (route.query?.redirect) {
      router.push({
        path: <string>route.query?.redirect,
        query: Object.keys(<string>route.query?.params).length > 0 ? JSON.parse(<string>route.query?.params) : '',
      });
    } else {
      router.push('/');
    }
    // 登录成功提示
    const signInText = t('signInText');
    useMessage().success(`${currentTimeInfo}，${signInText}`);
    // 添加 loading，防止第一次进入界面时出现短暂空白
    NextLoading.start();
  }
};

// 页面加载时
onMounted(() => {
  NextLoading.done();
});
</script>
<style scoped lang="scss">
.login-container01{
  height: 100vh;
  display: flex;
  justify-content: center;
  align-content: center;
  position: relative;
  z-index: 10;
  .login-box{
    .login-form{
      background-color: #fff;
      border-radius:8px;
      box-shadow: 0 7px 25px rgba(0, 0, 0, .08);
      position: relative;
      .logo-wrap{
        position: absolute;
        z-index: 10;
        left: 50%;
        top: -60px;
        transform: translateX(-50%);
        width: 110px;
        height: 110px;
        display: flex;
        justify-content: center;
        align-items: center;
        background-color: #fff;
        border-radius:50%;
        box-shadow: 0 4px 40px rgba(0, 0, 0, .07);
        .logo{
          width: 70px;
        }
      }
      .bg-white{
        padding: 0 48px 48px 48px;
      }
      .title{
        margin-top:70px ;
      }
    }
  }
  .copyright{
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
    z-index: 10;
    bottom: 20px;
    color: #999;
  }
}
</style>
